package com.servicelive.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;

import com.servicelive.ServiceLiveCommonService.OrderedItemDetails;
import com.servicelivedb.ServiceliveDB;
import com.servicelivedb.data.Customer;
import com.servicelivedb.data.Items;
import com.servicelivedb.data.Order;
import com.servicelivedb.data.OrderItems;
import com.wavemaker.runtime.RuntimeAccess;
import com.wavemaker.runtime.javaservice.JavaServiceSuperClass;
import com.wavemaker.runtime.service.annotations.ExposeToClient;

/**
 * This is a client-facing service class. All public methods will be exposed to
 * the client. Their return values and parameters will be passed to the client
 * or taken from the client, respectively. This will be a singleton instance,
 * shared between all requests.
 * 
 * To log, call the superclass method log(LOG_LEVEL, String) or log(LOG_LEVEL,
 * String, Exception). LOG_LEVEL is one of FATAL, ERROR, WARN, INFO and DEBUG to
 * modify your log level. For info on these levels, look for tomcat/log4j
 * documentation
 */
@ExposeToClient
public class OrderService extends JavaServiceSuperClass {
	/*
	 * Pass in one of FATAL, ERROR, WARN, INFO and DEBUG to modify your log
	 * level; recommend changing this to FATAL or ERROR before deploying. For
	 * info on these levels, look for tomcat/log4j documentation
	 */
	public OrderService() {
		super(INFO);
	}

	public String placeOrder(OrderDetails order, Integer vendorId) {

		String result = "";
		ServiceliveDB db = (ServiceliveDB) RuntimeAccess.getInstance()
				.getServiceBean("serviceliveDB");

		try {
			System.out.println("vendorId=" + vendorId);
			System.out.println("Items=" + order.getDetails());
			System.out.println("customerId=" + order.getCustomerId());

			db.begin();

			Session session = db.getDataServiceManager().getSession();

			Customer vendor = (Customer) session.get(Customer.class, vendorId);
			Customer customer = (Customer) session.get(Customer.class,
					order.getCustomerId());

			Order customerOrder = new Order();
			customerOrder.setCustomerByCustomerId(customer);
			customerOrder.setCustomerByVendorId(vendor);
			customerOrder.setOrderDt(new Date());
			customerOrder.setUpdatedBy(customer.getEmail());
			customerOrder.setAddress(customer.getAddress());
			customerOrder.setStatus("NEW");
			customerOrder.setContactNum(customer.getPrimaryPhone());
			session.save(customerOrder);
			Float price = 0f;
			for (OrderedItemDetails od : order.getDetails()) {

				OrderItems orderItem = new OrderItems();
				Items item = (Items) session.get(Items.class, od.getItemId());

				orderItem.setOrder(customerOrder);
				orderItem.setQuantity(od.getQuantity());
				orderItem.setPrice(new BigDecimal(od.getOrderedPrice()));

				orderItem.setItems(item);
				customerOrder.getOrderItemses().add(orderItem);
				session.save(orderItem);

				price += od.getOrderedPrice();
			}
			customerOrder.setTotalPrice(new BigDecimal(price));
			session.saveOrUpdate(customerOrder);

			db.commit();
			log(INFO, "Order created successfully: Order number is:"
					+ customerOrder.getOrderId() + ";Customer Id,email="
					+ customer.getCustomerId() + "&" + customer.getEmail()
					+ ";VendorName=" + vendor.getName());

			result = "Order created successfully: Order number is#"
					+ customerOrder.getOrderId();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			result = "Error during order creation,Please try again";
			log(ERROR, "Error during placing oreder:", e);
		}

		return result;
	}

	public static class OrderDetails {

		private Integer customerId;

		private List<OrderedItemDetails> details;

		private Integer vendorId;

		public Integer getCustomerId() {
			return customerId;
		}

		public void setCustomerId(Integer customerId) {
			this.customerId = customerId;
		}

		public Integer getVendorId() {
			return vendorId;
		}

		public void setVendorId(Integer vendorId) {
			this.vendorId = vendorId;
		}

		public List<OrderedItemDetails> getDetails() {
			return details;
		}

		public void setDetails(List<OrderedItemDetails> details) {
			this.details = details;
		}

	}

}
